package com.meixiang.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.http.SslError;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.lzy.okhttputils.cache.CacheMode;
import com.meixiang.R;
import com.meixiang.activity.account.login.LoginActivity;
import com.meixiang.global.ContentHint;
import com.meixiang.main.MXApplication;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbToastUtil;


/**
 * 自定义webView，带进度条
 */
public class ProgressWebView extends WebView {

    public ProgressBar progressbar;

    public ProgressWebView(final Context context, AttributeSet attrs) {
        super(context, attrs);
        progressbar = new ProgressBar(context, null,
                android.R.attr.progressBarStyleHorizontal);
        progressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                10, 0, 0));

        Drawable drawable = ContextCompat.getDrawable(context, R.drawable.progress_bar_states);
        progressbar.setProgressDrawable(drawable);
        addView(progressbar);

        setWebChromeClient(new WebChromeClient());
        //是否可以缩放
        getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        getSettings().setSupportZoom(true);
        getSettings().setBuiltInZoomControls(true);
        //设置支持javascript
        getSettings().setJavaScriptEnabled(true);
        //设置网页内容适应屏幕
        getSettings().setLoadWithOverviewMode(true);
        getSettings().setDomStorageEnabled(true);
        getSettings().setUseWideViewPort(true);
        getSettings().setUserAgentString("MXSH/1.0");
        //取消长按事件(屏蔽复制、分享等)
        setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });

    }

    public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
            } else {
                if (progressbar.getVisibility() == GONE)
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
        lp.x = l;
        lp.y = t;
        progressbar.setLayoutParams(lp);
        super.onScrollChanged(l, t, oldl, oldt);
    }
}
